import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin,UserConfig } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite' //自动导入相关api
import Components from 'unplugin-vue-components/vite' //按需导入组件
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite';
export default defineConfig(({ command }): UserConfig => {
  return {
    main: {
      plugins: [externalizeDepsPlugin()]
    },
    preload: {
      plugins: [externalizeDepsPlugin()]
    },
    renderer: {
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src'),
          '@': resolve('src/renderer/src')
        }
      },
      plugins: [
        vue(),
        AutoImport({
          imports: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
          resolvers: [
            // 自动导入elementPlus组件
            ElementPlusResolver(),
            // 自动导入图标组件
            IconsResolver({
              prefix: 'Icon'
            })
          ],
          dts: resolve(__dirname, 'src/renderer/src/types/auto-imports.d.ts')
        }),
        Components({
          resolvers: [
            ElementPlusResolver(),
            // 自动注册图标组件
            IconsResolver({
              enabledCollections: ['ep']
            })
          ],
          dts: resolve(__dirname, 'src/renderer/src/types/components.d.ts')
        }),
        createSvgIconsPlugin({
          // 指定需要缓存的图标文件夹
          iconDirs: [resolve(__dirname, 'src/renderer/src/assets/icons/svg')],
          // 指定symbolId格式
          symbolId: 'icon-[dir]-[name]',
          svgoOptions: command === 'build'
        }),
        VueI18nPlugin({
          include: [resolve(__dirname, 'src/renderer/src/lang/**.json')]
        }),
        //图标的导入配置
        Icons({
          autoInstall: true
        })
      ],
      css: {
        preprocessorOptions: {
          scss: {
            javascriptEnabled: true,
            api: 'modern-compiler'
          }
        },
        postcss: {
          plugins: [
            {
              postcssPlugin: 'internal:charset-removal',
              AtRule: {
                charset: (atRule) => {
                  if (atRule.name === 'charset') {
                    atRule.remove()
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
})
